﻿using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;
using DAL;
using DevExpress.Web.ASPxEditors;
public partial class EditConsumer : Frame.Pages.BaseInfomationEditBasePage, Frame.Pages.IDetailEditManPage<BI_Consumer>
{

    protected override bool IsAddCommandColumn
    {
        get
        {
            return false;
        }
    }

    /// <summary>
    /// 权限编码，需要重新设置
    /// </summary>
    protected override string ActionID
    {
        get { return "0"; }
    }

    protected string GetCompanyName(object CompanyID)
    {
        if (Cache["CompanyList"] == null)
        {
            using (DAL.CMSDALDataContext context = new DAL.CMSDALDataContext())
            {
                Cache["CompanyList"] = context.BI_Company.ToList();
            }
        }
        List<BI_Company> list = Cache["CompanyList"] as List<BI_Company>;
        BI_Company model = list.FirstOrDefault(o => o.CompanyID == (int)CompanyID);
        if (model != null)
        {
            return model.CompanyName;
        }
        return string.Empty;
    }
    protected void gridView_HtmlRowPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data)
        {
            if (e.GetValue("IsVip").ToString() == "1")
            {
                e.Row.BackColor = System.Drawing.Color.Pink;
            }
        }
    }
    protected void gridView_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
        List<string> IDlist = gridView.GetSelectedFieldValues("ConsumerID").Select<object, string>(o => o.ToString()).ToList();
        using (CMSDALDataContext context = new CMSDALDataContext())
        {
            foreach (string item in IDlist)
            {

                BI_Consumer model = context.BI_Consumer.FirstOrDefault(o => o.ConsumerID == long.Parse(item));
                context.BI_Consumer.DeleteOnSubmit(model);
            }
            context.SubmitChanges();
        }
        this.gridView.DataBind();
    }
    protected void btn_Click(object sender, EventArgs e)
    {
        ASPxButton btn = sender as ASPxButton;
        if (btn == null)
        {
            return;
        }
        else
        {
            BackUrl = "~/ConsumerManage/EditConsumer.aspx";
            ScriptBackUrl = "../ConsumerManage/EditConsumer.aspx";
            using (CMSDALDataContext dcontext = new CMSDALDataContext())
            {
                Model = dcontext.BI_Consumer.FirstOrDefault(o => o.ConsumerID == int.Parse(btn.CommandArgument));
                if (Model == null)
                {
                    Model = new BI_Consumer();
                }
            }
            switch (btn.CommandName.ToLower())
            {
                case "book":
                    {
                        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                        Response.Redirect("~/ConsumerManage/EditConsumerBook.aspx");
                        break;
                    }
                case "consume":
                    {
                        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                        Response.Redirect("~/ConsumerManage/EditConsumerHis.aspx");
                        break;
                    }
                case "returnvisit":
                    {
                        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                        Response.Redirect("~/BLIInfo/EditReturnVisit.aspx");
                        break;
                    }
                case "mday":
                    {
                        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                        Response.Redirect("~/BLIInfo/EditConsumerMDay.aspx");
                        break;
                    }
                case "sms":
                    {
                        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                        Response.Redirect(string.Format("~/SMSManage/EditSMSSendDetail.aspx?ConsumerID={0}", btn.CommandArgument));
                        break;
                    }
                case "update":
                    {
                        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
                        Response.Redirect("~/ConsumerManage/EditDetailConsumer.aspx");
                        break;
                    }
                default:
                    break;
            }
        }
    }

    public int ComID
    {
        get
        {
            int i = 0;
            if (int.TryParse(Request.QueryString["comid"], out i))
            {
                return i;
            }
            else
            {
                return -1;
            }
        }
    }
    protected void LSMDSourceMain_Selecting(object sender, DevExpress.Data.Linq.LinqServerModeDataSourceSelectEventArgs e)
    {
        if (ComID != -1)
        {
            DAL.CMSDALDataContext context = new CMSDALDataContext();
            e.QueryableSource = context.BI_Consumer.Where(o => o.RefrenceCompany_ID == ComID);
        }
        else
        {
            if (CMSUser.Current.IsAdmin)
            {
                DAL.CMSDALDataContext context = new CMSDALDataContext();
                e.QueryableSource = context.BI_Consumer;
            }
            else
            {
                DAL.CMSDALDataContext context = new CMSDALDataContext();
                e.QueryableSource = context.BI_Consumer.Where(o => o.UserID == CMSUser.Current.ID || o.UserID == -1);
            }
        }
    }

    #region IDetailEditManPage<BI_Consumer> 成员

    public string BackUrl
    {
        get;
        set;
    }

    public BI_Consumer Model
    {
        get;
        set;
    }

    public string ScriptBackUrl
    {
        get;
        set;
    }

    #endregion
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        System.Web.UI.WebControls.LinkButton btn = sender as System.Web.UI.WebControls.LinkButton;
        BackUrl = "~/ConsumerManage/EditConsumer.aspx";
        ScriptBackUrl = "EditConsumer.aspx";
        Frame.Pages.RefrenceContext<DAL.BI_Consumer>.UpdateCurrent(this);
        Response.Redirect(string.Format("~/ConsumerManage/EditDetailConsumer.aspx?KeyID={0}", btn.CommandArgument));
    }
    protected void BtnNew_Click(object sender, EventArgs e)
    {

        Response.Redirect("~/ConsumerManage/ConsumerSearchEdit.aspx");
    }


    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);
        if (ComID >= 0)
        {
            lblHeader.Text = " 企业 [" + GetCompanyName(ComID) + "] 授权人信息管理";
            gridView.Columns["RefrenceCompany_ID"].Visible = false;
        }
    }
}
